package com.tencent.sr.iris.activity.service.convert;

import cn.hutool.core.date.DateUtil;
import com.tencent.sr.iris.activity.dependency.dto.activity.QueryAwardReceiveListDTO;
import com.tencent.sr.iris.activity.interfaces.response.activity.QueryAwardReceiveListResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * @author nixiaolin
 * @date 2022/7/22 10:56
 */
@Mapper(componentModel = "spring", imports = {Date.class, Objects.class, Optional.class, DateUtil.class})
public abstract class ActivityQufaConvert {

    @Mapping(target = "awardRecordTime", expression = "java(getAwardRecordTime(dto))")
    @Mapping(target = "consumeTime", expression = "java(getConsumeTime(dto))")
    public abstract QueryAwardReceiveListResponse buildResponseFromDTO(QueryAwardReceiveListDTO dto);

    public Date getConsumeTime(QueryAwardReceiveListDTO dto) {
        if (dto != null && dto.getAwardRecord() != null
                && dto.getAwardRecord().getAwardRecordWechatCoupon() != null) {
            return Optional.ofNullable(dto.getAwardRecord().getAwardRecordWechatCoupon().getConsumeTime())
                    .map(DateUtil::parseDateTime).orElse(null);
        }

        return null;
    }

    public Date getAwardRecordTime(QueryAwardReceiveListDTO dto) {
        if (dto != null && dto.getAwardRecord() != null) {
            return Optional.ofNullable(dto.getAwardRecord().getAwardRecordTime())
                    .map(DateUtil::parseDateTime).orElse(null);
        }

        return null;
    }
}
